package com.hfzy.ihk.web.callhistoryold.controller.admin;

import com.hfzy.ihk.web.callhistoryold.biz.CallHistoryOldBiz;
import com.hfzy.ihk.web.callhistoryold.biz.CallhistoryPlayRecordBiz;
import com.hfzy.ihk.web.callhistoryold.constant.RedisKey;
import com.hfzy.ihk.web.callhistoryold.entity.CallHistoryOld;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.Set;

@Slf4j
@Controller
@RequestMapping("/adminCallHis")
public class AdminCallHistoryController {

    @Autowired
    CallHistoryOldBiz callHistoryOldBiz;

    @Autowired
    CallhistoryPlayRecordBiz callhistoryPlayRecordBiz;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/buildDatasFromHBase.do")
    @ResponseBody
    public void buildDatasFromHBase(int startUserId, int limit){
        log.info("开始:调用buildDatasFromHBase接口，startUserId:" + startUserId + ",limit:" + limit);
        long start = System.currentTimeMillis();

        callHistoryOldBiz.buildDatasFromHBase(startUserId,limit);

        log.info("结束:调用buildDatasFromHBase接口，startUserId:" + startUserId + ",limit:" + limit + ",耗时:" + (System.currentTimeMillis() - start));
    }


    @RequestMapping("/buildPlayRecordDatasFromHBase.do")
    @ResponseBody
    public void buildPlayRecordDatasFromHBase(int limit){
        log.info("开始:调用buildPlayRecordDatasFromHBase接口，limit:" + limit);
        long start = System.currentTimeMillis();

        callhistoryPlayRecordBiz.buildDatasFromHBase(limit);

        log.info("结束:调用buildPlayRecordDatasFromHBase接口，limit:" + limit + ",耗时:" + (System.currentTimeMillis() - start));
    }


    @RequestMapping("/buildRdUserIds.do")
    @ResponseBody
    public void buildRdUserIds(){
        log.info("开始执行:buildRdUserIds.do");
        stringRedisTemplate.opsForValue().set(RedisKey.IS_RUN_BUILD_CALLHIS,"0");
        RowMapper<CallHistoryOld> rowMapper = (RowMapper<CallHistoryOld>) BeanPropertyRowMapper
                .newInstance(CallHistoryOld.class);
        String sql = "select distinct reg_user_id  from mydba2.call_history_new  order by reg_user_id";
        List<Integer> list = jdbcTemplate.queryForList(sql,Integer.class);

        String[] uidArr = new String[list.size()];
        for(int i=0;i<list.size();i++){
            uidArr[i] = list.get(i) + "";

        }
        log.info("buildRdUserIds,查询出 userId:size:" + uidArr.length);
        stringRedisTemplate.opsForList().leftPushAll(RedisKey.CALLHIS_UID_LIST,uidArr);
        log.info("写入redis成功:userId共:" + uidArr.length + "个");
    }

    //在redis中从错误列表中重新建立userId列表
    @RequestMapping("/buildRdUserIdsFromEx.do")
    @ResponseBody
    public void buildRdUserIdsFromEx(Integer isRunBuildCallhis){
        log.info("开始执行:buildRdUserIdsFromEx.do");
        stringRedisTemplate.opsForValue().set(RedisKey.IS_RUN_BUILD_CALLHIS,"0");
        Set<String> uidSet = stringRedisTemplate.opsForSet().members(RedisKey.BUILD_CALLHIS_EX_UIDS);
        log.info("从redis错误列表中拿到:" + uidSet.size() + " 个记录)");
        stringRedisTemplate.opsForList().leftPushAll(RedisKey.CALLHIS_UID_LIST, uidSet);
        log.info("从redis错误列表写入uidList成功:userId共:" + stringRedisTemplate.opsForList().size(RedisKey.CALLHIS_UID_LIST) + "个");
        if(isRunBuildCallhis != null && isRunBuildCallhis == 1){
            stringRedisTemplate.opsForValue().set(RedisKey.IS_RUN_BUILD_CALLHIS,"1");
        }
    }
}
